-- Copyright (c) 2010, Pavel Kovar
-- All rights reserved.

---------------------------------------------------------------------------------------
-- This file is a part of the Witch Navigator project

-- PCIe transmitter power saving control


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

ENTITY PCIeCTRL IS	
    PORT (
        SIGNAL clk : IN std_logic;	
        SIGNAL rst_n : IN std_logic;	
        SIGNAL req_compl_i : IN std_logic;	
        SIGNAL compl_done_i : IN std_logic;	
        SIGNAL dma_run : IN std_logic;	
        SIGNAL cfg_to_turnoff_n : IN std_logic;	
        SIGNAL cfg_turnoff_ok_n : OUT std_logic);	
END PCIeCTRL;

ARCHITECTURE Behavioral OF PCIeCTRL IS

  SIGNAL trn_pending : std_logic; -- by zcz REGISTER ;	

BEGIN

--   Check if completion is pending
PROCESS(clk) 
  BEGIN
    if clk'event and clk ='1' then
      IF (rst_n = '0') THEN
        trn_pending <= '0';	
      ELSE
        IF (trn_pending = '0') AND (req_compl_i = '1') THEN
          trn_pending <= '1';	
        ELSIF compl_done_i = '1' THEN
          trn_pending <= '0';	
        END IF;
      END IF;
	 end if;	
  END PROCESS;

--   Turn-off OK if requested and no transaction is pending
PROCESS(clk) 
  BEGIN
    if clk'event and clk ='1' then
      IF (rst_n = '0') THEN
        cfg_turnoff_ok_n <= '1';	
      ELSE
        IF (cfg_to_turnoff_n = '0') AND (trn_pending = '0') AND (dma_run = '0') THEN
          cfg_turnoff_ok_n <= '0';	--power saving state, transmission is not possible
        ELSE
          cfg_turnoff_ok_n <= '1';	
        END IF;
      END IF;
    end if;
  END PROCESS;
end Behavioral;

